package com.example.myapplication.xiaochengxu;

import com.example.myapplication.Xinwen;
import com.example.myapplication.shortvideo.utils.GsonUtil;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okio.Utf8;

public class LOL {
    int index;

    public static void main(String[] args) {
        LOL jinPai = new LOL();
        jinPai.query();
//        new Timer().schedule(new TimerTask() {
//            @Override
//            public void run() {
//                try {
//                    LOL jinPai = new LOL();
//                    jinPai.query();
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
//            }
//        }, 0, 60 * 1000);
    }

    public static class Cos {
    }

    public void query() {
        String url = "https://aweme.snssdk.com/aweme/v1/play/?video_id=v0200fg10000cdh8rvrc77u3gscprba0&ratio=1080p&line=0";
        OkHttpClient okHttpClient = new OkHttpClient();
        final Request request = new Request.Builder()
                .url(url)
                .get()//默认就是GET请求，可以不写
                .addHeader("application/x-www-form-urlencoded", "charset=utf-8")
                .addHeader("Content-Type", "text/html; charset=utf-8")
                .build();
        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String str = response.body().string();
                String newStr = new String(str.getBytes(), "UTF-8");


                System.out.println(newStr);
//                fenxi(str);
            }
        });
    }

    private void fenxi(String html) {
        String tag1 = "opts.contentData =";
        int index1 = html.indexOf(tag1);
        if (index1 > 0) {
            String html2 = html.substring(index1 + tag1.length());
            String tag2 = "}};";
            int index2 = html2.indexOf(tag2);
            String html3 = html2.substring(0, index2 + tag2.length() - 1);
            save(html3);
        }
    }


    public void save(String json) {
        try {
            System.out.println(json);
            File file = new File("D:\\downloads\\putjson\\wzrank.json");
            if (file.exists()) {
                file.delete();
            }
            file.createNewFile();
            FileWriter fw = new FileWriter(file.getAbsoluteFile());
            BufferedWriter bw = new BufferedWriter(fw);
            bw.write(json);
            bw.close();
            System.out.println("保存成功");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
